<?php
/**
 * UserIdentity.php
 *
 * This class represents a user identity and this is able to authenticate a user.
 *
 * @author: antonio ramirez <antonio@clevertech.biz>
 * Date: 7/22/12
 * Time: 8:36 PM
 *
 *
 */
class UserIdentity extends CUserIdentity {
	/**
	 * @var integer id of logged user
	 */
      //inherited constants,good to know.
  /*  const ERROR_NONE=0;
    	const ERROR_USERNAME_INVALID=1;
    	const ERROR_PASSWORD_INVALID=2;
    	const ERROR_UNKNOWN_IDENTITY=100;*/
   	const ERROR_INACTIVE=3;
	const ERROR_SERVICE_CODE=3;
	private $_id;
	private $account_no;

	
	function UserIdentity($username, $password) {
		$this->username = $username;
		$this->password = $password;
		
	}
	
	/**
	 * Authenticates username and password
	 * @return boolean CUserIdentity::ERROR_NONE if successful authentication
	 */
       
	public function authenticate() {
		
		$ezApi = new EzRestApi();
		$user = null;
		try
		{
			$user = $ezApi->login($this->account_no, $this->username, $this->password);
                       
		}
		catch (Exception $e)
		{
			$this->errorCode = self::ERROR_USERNAME_INVALID;
			return !$this->errorCode;
		}
		//Only allow account S03(nâng cao) và S04(chuỗi) đăng nhập
		if ($user && ($user['serviceCode']!='S01' && $user['serviceCode']!='S02'))
		{
			$this->errorCode = self::ERROR_NONE;
			$this->_id = $user['userId'];
			$this->setState('userId', $user['userId']);
			$this->setState('userFullname', $user['userFullName']);
			$this->setState('accountId', $user['accountId']);
                        if(isset($_SESSION['id']) && $_SESSION['id']!='' ){
                            $user['locationId']=$_SESSION['id'];
                        }
			$this->setState('locationId', $user['locationId']);
			$this->setState('locationName', $user['locationName']);
			$this->setState('accountNo', $user['accountNo']);
			$this->setState('isAdmin', $user['isAdmin']);
                        
			$location = $ezApi->getLocation($user['accountId'], $user['locationId']);
                        
                        if(isset($_SESSION['id'])&& $_SESSION['id']!=''){
                            //$location['locationName']=$_SESSION['id'];
                            $location = $ezApi->getLocation($user['accountId'], $_SESSION['id']);
                        }
			if ($location)
			{
                            $this->setState('locationName', $location['locationName']);
			}
                       
		}
		else if ($user && ($user['serviceCode']=='S01' || $user['serviceCode']=='S02'))
		{
			$this->errorCode = self::ERROR_SERVICE_CODE;
		}
		else {
			$this->errorCode = self::ERROR_INACTIVE;
		}
		return !$this->errorCode;
	}

	/**
	 * Creates an authenticated user with no passwords for registration
	 * process (checkout)
	 * @param string $username
	 * @return self
	 */
	public static function createAuthenticatedIdentity($id, $username) {
		$identity = new self($username, '');
		$identity->_id = $id;
		$identity->errorCode = self::ERROR_NONE;
		return $identity;
	}

	/**
	 *
	 * @return integer id of the logged user, null if not set
	 */
	public function getId() {
		return $this->_id;
	}
}